<template>
	<view class="page">
      <view class="padding flex flex-wrap justify-between align-center ">
 			<input class="search-text" type="text" v-model="keyword" placeholder="输入品牌,型号" />
			<button class="cu-btn round bg-orange" @click="search">搜索</button>  
 			 
  </view>
 
		<view class="content" >
			<view class="uni-list">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item,index) in cars" :key="index"  @tap="openinfo"   :data-carid="item.id">
					<view class="uni-media-list">
						<image class="listimg" :src="item.firstImg"></image>
						<view class="uni-media-list-body"> 
							<view class="uni-media-list-text-top">{{item.brandName}}  {{item.xingHao}}</view>
							<view class="uni-media-list-text-bottom uni-ellipsis">
							{{item.speedAssembly}}   {{item.regdate.substring(0,4)}}年/{{item.Wkm}}万公里
							 <view class="sellprice"> {{item.showPrice/10000}}万</view> 
							
							<view >
								 <button class="cu-btn round bg-olive  btnclss"  v-if="item.sellState!=3"  :data-carid="item.id" @tap.stop="carEdit">编辑</button>   
								 <button class="cu-btn round bg-orange  btnclss" v-if="item.sellState==1" :data-carid="item.id"  @tap.stop="showModal">下架</button>  
								 <button class="cu-btn round bg-blue  btnclss" v-if="item.sellState==2" :data-carid="item.id"  @tap.stop="UpShelf">上架</button>
								
								   <text  v-if="item.sellState==3" class="bg-olive" round> 已售 </text>
								   <text v-if="item.sellState==3"> {{item.closePrice}} </text>
								 <button class="cu-btn round bg-red  btnclss" :data-carid="item.id" @tap.stop="CarDel">删除</button>  										 
							</view>
							
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		
		<view class="cu-modal" :class="modalName=='DialogModal1'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">确定下架吗</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					<view> 未成交请无须输入 </view>
					<view class="cu-form-group margin-top">
						<view class="title">成交价格</view>
						<input placeholder="" type="number" v-model="closePrice" name="input"></input>
					</view>					
					<view class="cu-form-group margin-top" @tap="hideModal()">
						<view class="title">提成人员</view>
						<picker @change="shopUserChange" :value="userIndex" :range="shopUser">
							<view class="picker">
								{{shopUser[userIndex]}}
							</view>
						</picker>
					</view>
					<view class="cu-form-group margin-top">
						<view class="title">提成金额</view>
						<input placeholder="" type="number" v-model="DudectMoney" name="input"></input>
					</view>
					
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-green text-green" @tap="hideModal">取消</button>
						<button class="cu-btn bg-green margin-left" @tap="pullDown">确定</button>
		
					</view>
				</view>
			</view>
		</view>
		
	</view>
	
	
</template>

<script> 
 import service from '../../service.js';
	 var _self;
	export default {
		data() {
			return {
				refreshing: false,
				title: 'Hello',
				cars: [],
				fetchPageNum: 1,
				keyword:'',
				userinfo:service.getUsers(),
				modalName: null,
				closePrice:0,
				currentid:0,
				shopUser: [],
				userIndex:0,
				DudectMoney:0   //提成金额
			}
		},
		onLoad(e) {
			_self=this;
			setTimeout(() => {
				this.getData();
			}, 300);
			//1.加载本店人员.
			console.log(service.getUsers());
			 uni.request({
			 	url: service.BaseUrl+'api/apicar/GetshopUser?shopid='+service.getUsers().Shopid,
			 	method: 'GET',
			 	success: res => {
					console.log(res.data);
			 			if (res.data.Code==1) {
			 				this.shopUser=res.data.Data;
			 			} 
			 	}
			 });
			 
		
		},
		onPullDownRefresh() {
			console.log('顶部下拉刷新整个页面');
			this.refreshing = true;
			this.getData();
		},
		onReachBottom() {
			console.log('底部上拉加载分页');
			if (this.fetchPageNum > 100) {
				this.loadMoreText = '不再加载更多了'
				return;
			}
			this.getData();
		},
		methods: {
			getData(e) {
				uni.request({
					url: service.BaseUrl+'api/apiCar/GetCarByUserID',
					data: {
						PageIndex:_self.fetchPageNum,
						PageSize:8 ,
						keyword:_self.keyword,
						Token:_self.userinfo.Token
						},
					method: 'POST',
					success: (ret) => {
						if (ret.statusCode !== 200 && ret.data.Code!=1) {
							console.log('请求失败', ret)
							return;
						}
						const data = ret.data.Data.data;
						// if (this.refreshing && data[0].id === this.cars[0].id) {
						// 	uni.showToast({
						// 		title: '已经最新',
						// 		icon: 'none',
						// 	});
						// 	this.refreshing = false;
						// 	uni.stopPullDownRefresh();
						// 	return;
						// }
						let list = [];
						for (var i = 0; i < data.length; i++) {
							var item = data[i];
							list.push(item);
						}
			
						if (this.refreshing) {
							this.refreshing = false;
							uni.stopPullDownRefresh();
							this.cars = list;
							this.fetchPageNum = 1;
						} else {
							this.cars = this.cars.concat(list);
							this.fetchPageNum += 1;
						}
					}
				});
			},		
			search(){
					this.refreshing = true;
				this.fetchPageNum=1;
			     this.getData();	
			},
			openinfo(e){
				var carid= e.currentTarget.dataset.carid;
				uni.navigateTo({
					url: '../carinfo/editCar?carid='+carid,
				});
			},
			carEdit(e){
				var carid= e.currentTarget.dataset.carid;
				uni.navigateTo({
					url: '../carinfo/editCar?carid='+carid,
				});
			}
			,showModal(e) {
				var carid= e.currentTarget.dataset.carid;
				_self.currentid=carid;
				this.modalName ="DialogModal1"
			}
			,pullDown(){
				uni.request({
					header:{ 'Authorization' : 'Basic '+service.getUsers().Token},
					url: service.BaseUrl+'api/apiCar/pullDown',
					method: 'POST',
					data: {
						ID:_self.currentid,
						closePrice: _self.closePrice,
						DudectMoney:_self.DudectMoney,
						sellUserName:_self.shopUser[_self.userIndex],
						Token:_self.userinfo.Token
					},
					success: res => {
						if(res.data.Code==1){
							var dt=res.data.Data;
							this.modalName = null;
						    this.getData();	
							uni.showToast({
								title:res.data.Msg
							}) 
						}
						else{
							uni.showToast({title:"下架失败"}) 
						}
					}
				});
			}
			,UpShelf(e){
				uni.request({
					header:{ 'Authorization' : 'Basic '+service.getUsers().Token},
					url: service.BaseUrl+'api/apiCar/UpShelf',
					method: 'POST',
					data: {
						ID:e.currentTarget.dataset.carid,
						Token:_self.userinfo.Token
					},
					success: res => {
						if(res.data.Code==1){
							var dt=res.data.Data; 
							this.search();
							uni.showToast({
								title:res.data.Msg
							})
						}
						else{
							uni.showToast({title:"上架失败"}) 
						}
					}
				});
			}
			,CarDel(e){
				uni.showModal({ //提醒用户更新
				    title: "确定删除吗",  
				    content: "确定删除该车吗",  
				    success: function (rmd)  {  
				        if (rmd.confirm) {  
				            uni.request({
				            	header:{ 'Authorization' : 'Basic '+service.getUsers().Token},
				            	url: service.BaseUrl+'api/apiCar/Del',
				            	method: 'POST',
				            	data: {
				            		ID:e.currentTarget.dataset.carid,
				            		Token:_self.userinfo.Token
				            	},
				            	success: res => {
				            		if(res.data.Code==1){
				            			var dt=res.data.Data; 
				            			this.search();
				            			uni.showToast({
				            				title:res.data.Msg
				            			})
				            		}
				            		else{
				            			uni.showToast({title:"删除失败"}) 
				            		}
				            	}
				            });
				        }  
				    }  
				});
				
				
			}
			,
			hideModal(e) {
				this.modalName = null;
			},
			shopUserChange(e){
				this.modalName ="DialogModal1"
				this.userIndex = e.detail.value
			}
			,	openinfo(e){
				var carid= e.currentTarget.dataset.carid;
				// uni.showToast({
				// 	title:carid
				// })
				uni.navigateTo({
					url: '../carinfo/carDetail?carid='+carid,
				});
			}
		}
	}
</script>


<style> 

 .uni-media-list-body{height: auto;	}
	 .uni-media-list-text-top{line-height: 2.6em;}
	 .uni-media-list-text-bottom{
	 		font-size: 30upx;
	 }
	

/* 列表样式 */

	.listimg{
		width: 270upx;
		height: 190upx;
		margin-right: 10upx;
		margin-left: 0upx;
	}
	
	.sellprice{
		color: #f96e39;
		font-size: 37upx;
		
	}
	
	.btnclss{
		 padding-left: 20upx;
		margin-left: 20upx;
	}
	
</style>
